package com.photoeditor.demo.ui.widget.blur;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

import com.common.base.utils.DeviceUtils;

/**
 * Created by winn on 17/5/16.
 */

public class LinearBlurTipView extends View {
    private Paint mPaint;
    private float mFocusX;
    private float mFocusY;
    private float mAngle;
    private float mWidth;
    private int mMaxWidth;

    private Path mFirstLinePath = new Path();
    private Path mSecondLinePath = new Path();

    public LinearBlurTipView(Context context) {
        super(context);
        init();
    }

    public LinearBlurTipView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LinearBlurTipView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(DeviceUtils.dip2px(getContext(), 2));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        PathEffect effects = new DashPathEffect(new float[]{DeviceUtils.dip2px(getContext(), 8), DeviceUtils.dip2px(getContext(), 4)}, 1);
        mPaint.setPathEffect(effects);
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    public void onUpdateWidth(float width) {
        mWidth = width;
        resetLine();
        invalidate();
    }

    public void onShowLinearBlurEffect(float angle, float focusX, float focusY, float width) {
        mAngle = angle;
        mFocusX = focusX;
        mFocusY = focusY;
        mWidth = width;
        resetLine();
        invalidate();
    }

    public void updateLinearBlurExclude(float angle, float focusX, float focusY, float width) {
        mAngle = angle;
        mFocusX = focusX;
        mFocusY = focusY;
        mWidth = width;
        resetLine();
        invalidate();
    }

    private void resetLine() {
        int startY = (int) (mFocusY - getWidth() * mWidth / 2);
        int endY = (int) (mFocusY + getWidth() * mWidth / 2);
        int startX = (int) (mFocusX - mMaxWidth);
        int endX = (int) (mFocusX + mMaxWidth);
        mFirstLinePath.reset();
        mFirstLinePath.moveTo(startX, startY);
        mFirstLinePath.lineTo(endX, startY);
        mSecondLinePath.reset();
        mSecondLinePath.moveTo(startX, endY);
        mSecondLinePath.lineTo(endX, endY);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mMaxWidth = (int) Math.sqrt(w * w + h * h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mFocusX == -1 && mFocusY == -1) {
            mFocusX = getWidth() / 2;
            mFocusY = getHeight() / 2;
            resetLine();
        }
        canvas.save();
        canvas.rotate(mAngle, mFocusX, mFocusY);
        super.onDraw(canvas);
        canvas.drawPath(mFirstLinePath, mPaint);
        canvas.drawPath(mSecondLinePath, mPaint);
        canvas.restore();
    }
}
